﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Configuration;
using System.Runtime.Serialization;
using Microsoft.Http;
using System.Runtime.Serialization.Json;

namespace ScrumFactory.Services.Clients {    

    [Export(typeof(IProjectsService))]
    public class ProjectsServiceClient : IProjectsService
    {
       
        private Uri baseUrl;

        [ImportingConstructor()]
        public ProjectsServiceClient([Import("ProjectsServiceUrl")] string url)
            : this (new Uri(url))
        {
        }

        public ProjectsServiceClient(Uri url)
        {
            this.baseUrl = url;
        }

        public ICollection<Project> GetAllProjects()
        {
            var client = new HttpClient(new Uri(this.baseUrl, "Projects"));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);           
            return response.Content.ReadAsJsonDataContract<ICollection<Project>>();                        
        }

        public ICollection<Project> GetRunningProjects() {
            var client = new HttpClient(new Uri(this.baseUrl, "Projects/?filter=Running"));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);       
            return response.Content.ReadAsJsonDataContract<ICollection<Project>>();
        }

        public Project GetProject(string projectUId)
        {
            var client = new HttpClient(new Uri(this.baseUrl, "Projects/" + projectUId));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);
            Project p = response.Content.ReadAsJsonDataContract<Project>();
            
            // need to fix the references, i did not figure out how to do this in a better way
            foreach (Sprint s in p.Sprints)
                s.Project = p;                
            return p;
        }

        public ICollection<Role> GetProjectRoles(string projectUId) {
            var client = new HttpClient(new Uri(this.baseUrl, "Projects/" + projectUId.Trim() + "/Roles"));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);
            return response.Content.ReadAsJsonDataContract<ICollection<Role>>();
        }

        public void CreateProjectRole(string projectUId, Role role) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.POST, new Uri(this.baseUrl, "Projects/" + role.ProjectUId.Trim() + "/Roles"), HttpContentExtensions.CreateJsonDataContract(role));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

        public void UpdateProjectRole(string projectUId, Role role) {
            var client = new HttpClient();
            
            HttpResponseMessage response = client.Send(HttpMethod.PUT, new Uri(this.baseUrl, "Projects/" + role.ProjectUId.Trim() + "/Roles"), HttpContentExtensions.CreateJsonDataContract(role));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

        public void DeleteProjectRole(string projectUId, string roleUId) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.DELETE, new Uri(this.baseUrl, "Projects/" + projectUId.Trim() + "/Roles/" + roleUId));
            RestServiceUtil.HandleHTTPErrorCode(response);         
        }

        public void CreateProject(Project project)
        {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.POST, new Uri(this.baseUrl,  "Projects"), HttpContentExtensions.CreateJsonDataContract(project));
            RestServiceUtil.HandleHTTPErrorCode(response);                   
        }

        public void UpdateProject(Project project) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.PUT, new Uri(this.baseUrl, "Projects"), HttpContentExtensions.CreateJsonDataContract(project));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

        public void AddSprint(string projectUId, Sprint sprint) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.POST, new Uri(this.baseUrl, "Projects/" + projectUId.Trim() + "/Sprints"), HttpContentExtensions.CreateJsonDataContract(sprint));
            RestServiceUtil.HandleHTTPErrorCode(response);
        }

        public ICollection<Sprint> UpdateSprint(string projectUId, Sprint sprint) {
            var client = new HttpClient();
            HttpResponseMessage response = client.Send(HttpMethod.PUT, new Uri(this.baseUrl, "Projects/" + projectUId.Trim() + "/Sprints"), HttpContentExtensions.CreateJsonDataContract(sprint));
            RestServiceUtil.HandleHTTPErrorCode(response);
            return response.Content.ReadAsJsonDataContract<ICollection<Sprint>>();
        }

        public ICollection<Sprint> GetSprints(string projectUId) {
            var client = new HttpClient(new Uri(this.baseUrl, "Projects/" + projectUId.Trim() + "/Sprints"));
            HttpResponseMessage response = client.Send(HttpMethod.GET);
            RestServiceUtil.HandleHTTPErrorCode(response);
            return response.Content.ReadAsJsonDataContract<ICollection<Sprint>>();

        }
    }
}
